<template>
  <el-dialog title="选择老师" :visible.sync="visible" width="800px" :close-on-click-modal="false"
    :close-on-press-escape="false" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="校区" prop="schoolName">
        <el-input v-model="queryParams.schoolName" readonly="readonly" @click.native="openSelectSchool(1)"
          placeholder="请输入校区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="queryParams.tel" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省、市、区" prop="queryProvinceAndCityAndDistrict">
        <el-cascader placeholder="请选择省、市、区" v-model="queryParams.queryProvinceAndCityAndDistrict" :options="options"
          @keyup.enter.native="handleQuery" @lazy-load="selectLoadOptions"
          :props="{ lazy: true,checkStrictly:true,multiple: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
          clearable></el-cascader>
      </el-form-item>
      <el-form-item label="上线" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" ref="selectTeacherTable" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="校区" align="center" prop="schoolName" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="联系电话" align="center" prop="tel" />
      <el-table-column label="省、市、区" align="center" prop="provinceAndCityAndDistrict" />
      <el-table-column label="上线" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_state" :value="scope.row.state" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectTeacherData">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listTeacher
  } from "@/api/bus_web/school/teacher";
  import {
    loadOptions
  } from "@/api/system/sys_region";
  import {
    mapState
  } from 'vuex';
  import store from "@/store";
  import SelectSchool from "../schoolinfo/select.vue"
  export default {
    name: "SelectTeacher",
    dicts: ['common_state'],
    components: {
      SelectSchool
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 是否显示校区选择
        selectFlag: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 教师管理表格数据
        teacherList: [],
        // 弹出层标题
        title: "",
        visible: false,
        options: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          schoolName: null,
          schoolIdArray: [],
          name: null,
          tel: null,
          province: null,
          city: null,
          district: null,
          address: null,
          state: null,
          queryProvinceAndCityAndDistrict: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {

        }
      };
    },
    computed: {
      ...mapState({
        school: state => state.selectSchool.school,
        schoolSelectModel: state => state.selectSchool.schoolSelectModel,

        teacher:state=>state.selectTeacher.teacher,
        teacherSelectModel:state=>state.selectTeacher.teacherSelectModel
      })
    },
    created() {
      this.getList();
    },
    methods: {
      init() {
        this.visible = true

        this.resetQuery()
      },
      /**省 市 区*/
      selectLoadOptions(node, resolve) {
        var query = {
          regionLevel: node.level + 1,
          parentId: node.value || null
        }
        loadOptions(query).then(response => {
          resolve(response.data);
        });
      },
      /** 查询教师管理列表 */
      getList() {
        this.loading = true;
        listTeacher(this.queryParams).then(response => {
          this.teacherList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.visible = false
        this.reset()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      //选择校区
      openSelectSchool(selectVal) {
        this.selectFlag = true
        // selectVal =1 查询模式 =2 创建、修改模式
        this.$store.dispatch('selectSchool/cleanSchool')
        this.$store.dispatch('selectSchool/setSchoolSelectModel', selectVal)
      },
      closeSelectSchool() {
        this.selectFlag = false
      },
      //确定校区
      selectSchoolData() {
        this.closeSelectSchool()
        if (this.schoolSelectModel == 2) {
          this.form.schoolName = this.school[0].schoolName
          this.form.schoolId = this.school[0].id
          this.$refs.form.clearValidate(['schoolName']);
        } else {
          this.queryParams.schoolName = this.school.map(item => item.schoolName).join(',')
          this.queryParams.schoolIdArray = this.school.map(item => item.id)
        }
        this.$store.dispatch('selectSchool/cleanSchool')
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        if (selection.length > 1 && this.teacherSelectModel == 2) {
          selection = []
          this.$refs.selectTeacherTable.clearSelection()
          this.$store.dispatch('selectTeacher/cleanTeacherData')
          this.$modal.msgError("最多只能选择1条数据")
          return
        }
        selection.map(item => {
          this.$store.dispatch('selectTeacher/setTeacher', item)
        })
      },
      selectTeacherData() {
        if (this.teacher.length < 1) {
          this.$modal.msgError("请至少选中1条教师")
          return
        }
        this.visible = false
        this.$emit('selected-data')
      }
    }
  };
</script>
